package com.zdtech.action;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.zdtech.common.DataResult;
import com.zdtech.entity.TMenu;
import com.zdtech.service.BaseSerive;
import com.zdtech.util.DateUtil;
@Controller
public class InsertMenuAction extends ActionSupport{
	
	private DataResult<TMenu> dataResult =new DataResult<>();
	private String no;
	private String name;
	private String price;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	private String intro;
	private String orderid;
	@Resource
	private BaseSerive baseService;
	
	public String execute() throws Exception {
		if(null!=no&&no.length()>0){
			if(null!=name&&name.length()>0){
				if(null!=price&&price.length()>0){
					if(null!=intro&&intro.length()>0){
						if(null!=orderid&&orderid.length()>0){
							if(null!=photo){
								TMenu menu = new TMenu();
								String realpath = ServletActionContext.getServletContext().getRealPath("/images");
								StringBuffer sb = new StringBuffer();
								sb.append(DateUtil.getCurrentDate("yyyyMMddHHmmss"));
								sb.append(photoFileName);
					            File savefile = new File(new File(realpath), sb.toString());
					            menu.setNo(no);
					            menu.setName(name);
					            menu.setPrice(Double.parseDouble(price));
					            menu.setPhoto(sb.toString());
					            menu.setIntro(intro);
					            menu.setOrderid(Integer.parseInt(orderid));
					            menu.setDel(0);
					            /*****未使用字段*********/
					            menu.setRecomm(0);
					            menu.setDetail(null);
					            menu.setRes1(null);
					            menu.setRes2(null);
					            menu.setRes3(null);
					            /*****未使用字段*********/
					            baseService.insertMenu(menu);
					            if (!savefile.getParentFile().exists()){
					                savefile.getParentFile().mkdirs();
					        	}
					            FileUtils.copyFile(photo, savefile);
					            System.out.println("菜品保存成功");
					            dataResult.setSuccess(true);
					            dataResult.setMessage("菜品保存成功");
							}else{
								System.out.println("photo error");
								dataResult.setSuccess(false);
								dataResult.setMessage("photo error");
							}
						}else{
							System.out.println("orderid error");
							dataResult.setSuccess(false);
							dataResult.setMessage("orderid error");
						}
					}else{
						System.out.println("info error");
						dataResult.setSuccess(false);
						dataResult.setMessage("info error");
					}
				}else{
					System.out.println("price error");
					dataResult.setSuccess(false);
					dataResult.setMessage("price error");
				}
			}else{
				System.out.println("name error");
				dataResult.setSuccess(false);
				dataResult.setMessage("name error");
			}
		}else{
			System.out.println("no error");
			dataResult.setSuccess(false);
			dataResult.setMessage("no error");
		}
		 return Action.SUCCESS;
	}

	public DataResult<TMenu> getDataResult() {
		return dataResult;
	}

	public void setDataResult(DataResult<TMenu> dataResult) {
		this.dataResult = dataResult;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}
}
